﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Task;
using Backend.JYD.Model;
using Backend.JYD.Model.Cust;

namespace Backend.JYD.EFDAL.DAL
{
    public class IntentEntryInfoDAL : IDAL.Task.IIntentEntryInfo
    {
        public long AddIntentEntryInfo(IntentEntryInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                db.IntentEntryInfoes.Add(info);
                db.SaveChanges();
                return info.IntentId;
            }
        }

        public bool UpdateIntentEntryInfo(IntentEntryInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.IntentEntryInfoes.Where( p=> p.IntentId == info.IntentId);
                if (query != null)
                {
                    var temp = query.FirstOrDefault();
                    if (temp != null)
                    {
                        temp.UpdateTime = DateTime.Now;
                        temp.OpId = info.OpId;
                        if (!string.IsNullOrWhiteSpace(info.CustName)) temp.CustName = info.CustName;
                        if (!string.IsNullOrWhiteSpace(info.Phone)) temp.Phone = info.Phone;
                        //CustNo不可改
                        temp.CustNo = info.CustNo;
                        temp.ChanelType = info.ChanelType;
                        temp.CustName = info.CustName;
                        temp.PaperNo = info.PaperNo;
                        temp.PaperType = info.PaperType;
                        temp.Phone = info.Phone;
                        temp.Guarantee = info.Guarantee;
                        temp.LoanMoney = info.LoanMoney;
                        temp.Purpose = info.Purpose;
                        temp.Rate = info.Rate;
                        temp.Remark = info.Remark;
                        temp.RepaymentType = info.RepaymentType;
                        temp.Term = info.Term;
                        temp.Status = info.Status;
                        temp.TaskDetailId = info.TaskDetailId;
                        temp.TaskId = info.TaskId;
                        temp.Status = (info.Status.HasValue && info.Status != 1) ? info.Status : 1;

                        return db.SaveChanges() > 0;
                    }
                }
                return false;
            }
        }

        public IList<IntentEntryInfo> GetIntentEntryInfoByEmpId(Int64 empId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.IntentEntryInfoes.Where(p => p.OpId == empId);

                return query == null ? null : query.ToList();
            }
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        public IntentEntryInfo GetIntentEntryInfoByIntentId(Int64 intentId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<IntentEntryInfo> query = db.IntentEntryInfoes.Where(p => p.IntentId == intentId);
                return query.FirstOrDefault();
            }
        }

        public IList<IntentEntryInfo> GetIntentEntryInfoByTaskIds(IList<Int64> taskIds)
        {
            Int64[] ids = taskIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<IntentEntryInfo> query = db.IntentEntryInfoes.Where(p => taskIds.Contains(p.TaskId));
                return query.ToList();
            }
        }

        public PageInfo<IntentEntryInfo> GetIntentEntryInfoByPage(Int64? empId, DateTime? start, DateTime? end, int status, PageInfo page)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<IntentEntryInfo> query = db.IntentEntryInfoes;
                if (empId.HasValue)
                    query = query.Where(p => p.OpId == empId);
                if (status != -1)
                {
                    if (status == 1)
                        query = query.Where(p => p.Status <= status);
                    else
                        query = query.Where(p => p.Status >= status);
                }
                if (start.HasValue)
                    query = query.Where(p => p.CreateTime >= start);
                if (end.HasValue)
                    query = query.Where(p => p.CreateTime <= end);

                return new PageInfo<IntentEntryInfo>(page, query);
            }
        }

        public PageInfo<CustBase> SearchCustBaseInfo(PageInfo page, string name, string phone, int? chanel)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<IntentEntryInfo> query = db.IntentEntryInfoes.Where(p => p.CustNo == null || p.CustNo == string.Empty);
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.Where(p => p.CustName.Contains(name));
                }
                if (!string.IsNullOrWhiteSpace(phone))
                {
                    query = query.Where(p => p.Phone == phone);
                }
                if (chanel.HasValue && chanel > 0)
                {
                    query = query.Where(p => p.ChanelType == chanel.Value);
                }
                //其他条件代码
                var temp = new PageInfo<IntentEntryInfo>(page, query);
                if (temp == null || temp.PageList == null)
                {
                    return new PageInfo<CustBase>(page);
                }
                return temp.CastHandle<CustBase>(ConvertIntentEntryInfo);
            }
        }

        private CustBase ConvertIntentEntryInfo(IntentEntryInfo info)
        {
            if (info == null) return null;
            
            CustBase cust = new CustBase();
            cust.ChanelType = info.ChanelType;
            cust.CustName = info.CustName;
            cust.CustNo = info.CustNo;
            cust.PaperNo = info.PaperNo;
            cust.PaperType = info.PaperType;
            cust.Phone = info.Phone;
            return cust;
        }
    }
}
